#include "mandate.h"

int pwd(int *id,char *buf_to_store_path,MYSQL* mysql){

    char sql[1024]={0};

    sprintf(sql,"select path from file_tbl where id = %d;",*id);

    int qret=mysql_query(mysql,sql);
    if(qret!=0){
        fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
    }

    MYSQL_RES* result = mysql_store_result(mysql);
    MYSQL_ROW row;
    row=mysql_fetch_row(result);

    strcpy(buf_to_store_path,row[0]);
    return 0;
}

int mkdir_server(char* dir_name,char* pwd,char* user_name,int* id,MYSQL* mysql){

    char sql[1024]={0};

    if(*id!=-1)
    {
        char *dir = strtok(dir_name,"/");
        strcat(pwd,dir);
        strcat(pwd,"/");
    }

    sprintf(sql,"insert into file_tbl (filename,user_name,pre_id,type,path,is_delete,md5)" 
            "values('%s','%s',%d,'dir','%s',0,'0');",dir_name,user_name,*id,pwd);
    printf("%s\n",sql);
    int qret=mysql_query(mysql,sql);
    if(qret!=0){
        fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
    }

    if(*id==-1)
    {
        bzero(sql,sizeof(sql));
        sprintf(sql,"select id from file_tbl where path = '%s';",pwd);

        qret=mysql_query(mysql,sql);
        if(qret!=0){
            fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
        }

        MYSQL_RES *res=mysql_store_result(mysql);
        MYSQL_ROW row;
        row=mysql_fetch_row(res);
        *id=atoi(row[0]);
    }

    return 0;
}

int remove_server(char* filename,char* pwd,char* user_name,MYSQL* mysql){

    char sql[1024]={0};
    sprintf(sql,"update file_tbl set is_delete = 1 where filenmae = '%s' and pwd = '%s' and user_name = '%s' and is_delete = 0;",filename,pwd,user_name);
    int qret=mysql_query(mysql,sql);
    if(qret!=0){
        fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
        return -1;
    }

    return 0;
}

int rmdir_server(char* filename,char* pwd,char* user_name,int* id,MYSQL* mysql){
    char sql[1024]={0};
    sprintf(sql,"select id from file_tbl where filename = '%s' and pre_id = '%d' and type = 'dir' and is_delete = 0;",filename,*id);
    int qret=mysql_query(mysql,sql);
    if(qret!=0){
        fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
        return -1;
    }
    MYSQL_RES* result = mysql_store_result(mysql);
    if(result == NULL)
    {
        return -1;
    }
    else
    {
        MYSQL_ROW delete_id = mysql_fetch_row(result);
        bzero(sql,sizeof(sql));
        sprintf(sql,"select id from file_tbl where pre_id = %d and is_delete = 0;",atoi(delete_id[0]));
        int qret=mysql_query(mysql,sql);
        if(qret!=0){
            fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
            return -1;
        }
        result = mysql_store_result(mysql);
        if(mysql_num_rows(result)==0)
        {
            bzero(sql,sizeof(sql));
            sprintf(sql,"update file_tbl set is_delete = 1 where id = %d;",atoi(delete_id[0]));
            qret=mysql_query(mysql,sql);
            if(qret!=0){
                fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
            }
            printf("dir with id= %d is deleted sucessfully!\n",atoi(delete_id[0]));
            return 0;
        }
        else
        {
            return -1;
        }
    }
}

int change_id(char* user_name,int *id,int pre_id,MYSQL* mysql){

    char sql[1024]={0};
    sprintf(sql,"select id from file_tbl where user_name = '%s' and pre_id = %d;",user_name,pre_id);

    int qret=mysql_query(mysql,sql);
    if(qret!=0){
        fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
    }

    MYSQL_RES *res=mysql_store_result(mysql);
    MYSQL_ROW row;
    row=mysql_fetch_row(res);
    *id=atoi(row[0]);

    return 0;
}

